package com.runjian.rundo.iot.agent.iec104.parser.frame.monitoring.process;

import com.runjian.rundo.iot.agent.iec104.parser.frame.ASDUFrame;
import com.runjian.rundo.iot.agent.iec104.parser.frame.InfoElement;

import cn.hutool.core.util.ByteUtil;
import com.runjian.rundo.iot.agent.iec104.parser.frame.monitoring.base.BaseMonitoringFrame;
import lombok.Getter;
import lombok.Setter;

/**
 * M_SP_NA_1(1, "(遥信)单点信息"),
 *
 * @author CaoQingHua
 */
@Getter
@Setter
public class M_SP_NA_1 extends BaseMonitoringFrame {

    @Override
    public void analysisInfo(byte[] msg) {
        // 不带时标的单双点遥信
        if (this.getSq()) {
            // 地址连续
            int sAddress = ByteUtil.bytesToInt(new byte[] {msg[12], msg[13], msg[14], 0x00});
            for (int i = 0; i < this.getNumber(); i++) {
                this.addInfoElement(InfoElement.<Short>builder().address(sAddress + i).value((short)(msg[15 + i] & 0xff)).build());
            }
        } else {
            for (int i = 0; i < this.getNumber(); i++) {
                this.addInfoElement(InfoElement.<Short>builder()
                    .address(ByteUtil.bytesToInt(new byte[] {msg[12 + (4 * i)], msg[13 + (4 * i)], msg[14 + (4 * i)], 0x00}))
                        //因为 address 占用 4 bit，所以 需要左移 4 位
                    .value((short)(msg[15 + (4 * i)] & 0xff)).build());
            }
        }
    }
}
